<template>
  <div class="app-container">
    <div class="top-container" style="display: flex;margin: 0 0 20px 0;width: 100%;">
      <div style="width: 100%;display: flex;justify-content: space-between;">
        <div style="width: 70%;">
          <el-input placeholder="请输入内容"  v-model="input3" class="input-with-select" style="width: 80%;">
            <el-select v-model="select" slot="prepend"  placeholder="请选择查询类型" style="width: 170px;">
              <el-option label="依据船只编号" value="0"></el-option>
              <el-option label="依据船只类型" value="1"></el-option>
              <el-option label="依据船龄" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="SearchRecords"></el-button>
          </el-input>
        </div>
        <div style="">
          <el-button type="success" @click="handleExport">导出Excel</el-button>
        </div>
      </div>
    </div>
    <div style="display: flex;width: 100vw;">
      <div style="width: 65%;">
        <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
          <el-table-column align="center" porp="id" label="船只编号"  width="105px" sortable>
            <template slot-scope="scope">
              {{ scope.row.id}}
            </template>
          </el-table-column>
          <el-table-column class-name="status-col" porp="type" label="船只类型" width="120" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status | statusFilter">{{ getTypeLabel(scope.row.type) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column class-name="status-col" porp="shipAge" label="船龄" width="110" align="center" sortable>
            <template slot-scope="scope">
              <el-tag :type="scope.row.status | statusFilter">{{ scope.row.shipAge}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="createTime" label="注册时间" width="200" sortable>
            <template slot-scope="scope">
              <i class="el-icon-time" />
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="small" type="danger" icon="el-icon-delete" circle
                @click="showDeleteDialog(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="width: 20%;">
        <el-card style="border-radius: 16px;width: 90%;margin-left: 30px;">
          <div slot="header" class="clearfix">
            <span style="font-weight: bold;font-size: 20px;">注册船只须知</span>
            <p style="color: grey;">船只注册是海洋捕捞的重要环节，请各位渔民严格遵循以下要求和注册格式规定，积极配合工作</p>
          </div>
          <div>
            <p>1. 如实上报船龄</p>
            <p>2. 大型渔船：船长>=24米</p>
            <p>3. 中型渔船：船长>=12米且&lt24米</p>
            <p>4. 小型渔船：船长&lt12米</p>
            <el-button type="danger" icon="el-icon-upload2" @click="dialogFormVisible = true">注册船只</el-button>
          </div>
        </el-card>
      </div>
    </div>
    <div style="margin-top: 40px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
        background :page-sizes="[10, 50, 100, 200]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!--点击删除记录时打开的对话框，不要放在table里，单独拿出来就行，不然有多少条记录就会渲染出多少个，非常卡-->
    <el-dialog title="正在执行有风险的操作" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span>您确定要将这艘船删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
 
      <el-button @click="deleteMyBoat" >删除</el-button>


      </span>
    </el-dialog>
     <!--点击上报打开的对话框，不要放在table里，单独拿出来就行，不然有多少条记录就会渲染出多少个，非常卡-->
     <el-dialog title="注册新渔船" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="船只编号" :label-width="formLabelWidth">
          <el-input v-model="form.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="船只类型" :label-width="formLabelWidth">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="船龄" :label-width="formLabelWidth">
          <el-input v-model="form.shipAge" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click="RegisterShip">注册</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { F_exportBoat } from '@/api/excel'
import {getShipList,deleteShip,registerShip,searchShipRecord} from '@/api/table'

//import {Edit, Delete,Search} from '@element-plus/icons-vue'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      input3: '',
      select: '',
      pageNum: 0, // 当前页码，通常初始化为1
      pageSize: 10, // 每页显示的记录数
      dialogVisible: false,
      dialogFormVisible: false,
      currentDeletingId: null, // 当前准备删除的船的ID
      form: {
        id:'',
        type: '',
        shipAge:''
      },
      formLabelWidth: '120px',
      options: [{
          value: '0',
          label: '大型渔船'
        }, {
          value: '1',
          label: '中型渔船'
        }, {
          value: '2',
          label: '小型渔船'
        }],
        value: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleExport(){
      let s = this.select
      let k = this.input3
      if (this.select === ''|| this.select === null || this.select === undefined) {
          s = -1
      }
      if (this.input3 === ''|| this.input3 === null || this.input3 === undefined) {
          k = "null"
      }
      return F_exportBoat({
        keyword:k,
        searchType:parseInt(s),
        status:parseInt(-1)}) // 渔民status恒为-1
    },
    showDeleteDialog(row) {
      this.currentDeletingId = row.id; // 设置当前准备删除的船的ID
      this.dialogVisible = true; // 显示对话框
      console.log(currentDeletingId);
    },
    getTypeLabel(typeValue) {
    const typeLabels = {
      '0': '大型渔船',
      '1': '中型渔船',
      '2': '小型渔船',
      // 如果有更多类型，请继续添加
    };
    return typeLabels[typeValue] || '未知类型'; // 如果typeValue不是0，1或2，则返回'未知类型'
  },
    RegisterShip(){
      registerShip(this.form).then(response => {
        if (response.code === 200) {
          this.$message.success(response.msg);
        }
        
        else{
          this.$message.error(response.msg);
        }
      })
    },
    SearchRecords() {
      this.listLoading = true;
      searchShipRecord({keyword:this.input3,
        pageNum:parseInt(this.pageNum),
        pageSize:parseInt(this.pageSize),
        searchType:parseInt(this.select)}).then(response=>{
        // 使用Object.entries()获取键值对数组
        const entries= Object.entries(response.data)
        console.log(entries)

        // 拿到total和num
        const [total,num]=entries[0]
        console.log("total=",total)
        console.log("value=",num)
        this.total=num
        
        //拿到list和数据
        const [key,value]=entries[1]
        console.log("key=",key)
        console.log("value=",value)

        this.list = value
        console.log("list=",this.list)
        this.listLoading = false
      });
    },
    fetchData() {
      this.listLoading = true
      getShipList(this.pageNum, this.pageSize)
        .then(response => {
          if (response.code === 200) {
            const entries= Object.entries(response.data)
            console.log(entries)

        // 拿到total和num
            const [total,num]=entries[0]
            console.log("total=",total)
            console.log("value=",num)
            this.total=num
        
        //拿到list和数据
            const [key,value]=entries[1]
            console.log("key=",key)
            console.log("value=",value)

            this.list = value
            console.log("list=",this.list)
            this.listLoading = false
          } else {
            // 处理错误情况
            console.error(response.msg);
          }
        })
        .catch(error => {
          console.error(error);
        });
    },
    deleteMyBoat(){

        deleteShip(this.currentDeletingId).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功');
            // 重新获取数据更新列表
            this.dialogVisible = false;
            this.fetchData();

          } else {
            this.$message.error(response.msg);
          }
        }).catch(error => {
          this.$message.error('删除失败');
          console.error('删除失败:', error);
        });
    }
    ,
    handleSizeChange(newSize) {
      // 当每页显示的记录数变化时触发
      this.pageSize = newSize;
      this.fetchData();
    },
    handleCurrentChange(newPageNum) {
      // 当页码变化时触发
      this.pageNum = newPageNum;
      this.fetchData();
    },
    test() {
      console.log(search_type)
      search_type = 'aaa'
      console.log(search_type)
    }
  }
}
</script>

<style>
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
